<?php
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
/**
 * 用户管理控制器
 */
class user extends general
{
    function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
        $this->navigation_current = 'user';
    }
    function index()
    {
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 10);

        $objUser = spClass("userModel");
        $arrUser = $objUser->spPager($current_page, $page_size)->findAll(null,
            'uid desc');
        $arrBar = $objUser->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->tUsers = $arrUser;
        $this->display("admin/user_index.html");
    }
    function add()
    {
        $objUser = spClass("userModel");
        $this->tUserEnabled = $objUser->userEnabled();
        $objUsergroup = spClass("usergroupModel");
        $this->tUserGroup = $objUsergroup->glist();
        $this->action = 'add';
        $this->addtitle = "添加新用户";
        $this->display("admin/user_add.html");
    }
    function edit()
    {
        $uid = $this->spArgs("uid");
        $objUser = spClass("userModel");
        $this->user = $objUser->userDetail($uid);
        $this->tUserEnabled = $objUser->userEnabled();
        $objUsergroup = spClass("usergroupModel");
        $this->tUserGroup = $objUsergroup->glist();
        $this->action = 'edit';
        $this->addtitle = "修改用户";
        $this->display("admin/user_add.html");
    }
    function profile()
    {
        $uid = $this->spArgs("uid");
        $objUser = spClass("userModel");
        $this->user = $objUser->userDetail($uid);
        $this->tUserEnabled = $objUser->userEnabled();
        $objUsergroup = spClass("usergroupModel");
        $this->tUserGroup = $objUsergroup->glist($this->getLang());
        $this->display("admin/user_profile.html");
    }
    function save()
    {
        $uid = $this->spArgs("uid");
        $strAction = $this->spArgs("action");
        $data = array(
            'uname' => $this->spArgs("uname"),
            'firstname' => $this->spArgs("firstname"),
            'lastname' => $this->spArgs("lastname"),
            'email' => $this->spArgs("email"),
            'enabled' => $this->spArgs("enabled"),
            'gid' => $this->spArgs("usergroup"),
            );
        $password = $this->spArgs("upass");
        $confirmpassword = $this->spArgs("confirmpassword");
        if ($password != '' && $password == $confirmpassword) {
            $data['upass'] = md5($password);
        }
        $objUser = spClass("userModel");
        if ($strAction == 'add') {
            $data['addtime'] = time(); //date('y-m-d H:i:s',time())
            $userExist = $objUser->userExist($data['uname'], $uid);
            if ($userExist) {

                $msg = "用户名" . $data['uname'] . "已存在";
            } else {

                $objUser->create($data);
                $msg = "新用户" . $data['uname'] . "添加成功";
            }
        }
        if ($strAction == 'edit') {
            $conditions = array('uid' => $uid);
            $objUser->update($conditions, $data);
            $msg = "用户" . $data['uname'] . "修改成功";
        }
        Savelog($msg);
        $result = array(
            'status' => 'y', // 标志
            'info' => $msg, // 提示信息
            );

        echo json_encode($result); // 返回（显示）JSON结果

    }
    function del()
    {

        $Obj = spClass("userModel");

        $uid2 = $this->spArgs("uid", 0);
        if ($uid2 == 0) {
            $selids = $this->spArgs("selids");
            $selarray = explode(',', $selids);

            foreach ($selarray as $uid) {
                $conditions = array('uid' => $uid);
                $Obj->delete($conditions); // 删除记录
            }
        } else {

            $conditions = array('uid' => $uid2);
            $Obj->delete($conditions); // 删除记录
        }

        $loginfo = "删除用户." . $uid2 . ".";
        Savelog($loginfo);
        $this->success(T('Successfully  deleted!'), spUrl("user", "index"));
    }

    // 退出登录
    function logout()
    {
        $loginfo = "退出后台.";
        Savelog($loginfo);
        $_SESSION = array();
        if (isset($_COOKIE['_auth'])) {
            setcookie('_auth', '', time() - LASTTIME,WEBDIR);
            setcookie('_FSESSID', '', time() - LASTTIME,WEBDIR);
        }
        session_destroy();
        $this->jump(spUrl("user", "login"));
    }

    // 用户登录框以及验证用户登录情况
    function login()
    {
        $userObj = spClass("userModel");
        if ($uname = $this->spArgs("username")) {
            $upass = $this->spArgs("passwd");
            $rows = array('uname' => $uname, 'upass' => upass);
            $results = $userObj->spVerifier($rows);
            if (false == $results) { // 当spVerifier返回false的时候，则是表示已经通过验证，数据是合格的
                if (false == $userObj->userlogin($uname, $upass)) {
                    $loginfo = $uname . "-" . $upass . " 登入失败.";
                    Savelog($loginfo);
                    // 登录失败，提示后跳转回登录页面
                    $this->error(T("The username address or password you provided does not match our records."),
                        spUrl("user", "login"));
                } else {
                    $loginfo = $uname . "登入成功.";
                    Savelog($loginfo);
                    $this->jump(spUrl('main', 'index'));
                }
            } else {
                foreach ($results as $item) {
                    foreach ($item as $msg) {
                        $this->error($msg, spUrl("user", "login"));
                    }
                }
            }
        }
        $this->display("admin/user_login.html");
    }
}
